<template>
  <view class="navbar" :style="{ paddingTop: safeAreaInsets.top + 'px' }">
    <!-- logo文字 -->
    <view class="logo">
      <image class="logo-image" src="@/static/images/logo.png"></image>
      <text class="logo-text">环保 · 亲民 · 快捷</text>
    </view>
    <!-- 搜索条 -->
    <view class="search">
      <input 
        class="search-input" 
        type="text" 
        placeholder="搜索商品"
        @confirm="handleSearch"
        v-model="searchText"
      />
      <image 
        class="search-icon" 
        :src="getSearchIcon()"
        @click="handleSearch"
      ></image>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync();
const searchText = ref('')
const searchState = ref('default') // 'default', 'searching', 'searched'

// 根据状态获取对应图标
const getSearchIcon = () => {
  switch (searchState.value) {
    case 'searching':
      return '/static/icon/search/search.png'
    case 'searched':
      return '/static/icon/search/search.png'
    default:
      return '/static/icon/search/default.png'
  }
}

// 搜索处理函数
const handleSearch = () => {
  if (searchText.value.trim()) {
    // 设置为搜索中状态
    searchState.value = 'searching'
    
    // 这里可以执行搜索逻辑，例如跳转到搜索结果页
    console.log('搜索内容:', searchText.value)
    // 示例：跳转到搜索结果页面
    uni.navigateTo({
      url: `/pages/mall/components/Search/Search?keyword=${encodeURIComponent(searchText.value)}`
    })
    
    // 设置为已搜索状态
    searchState.value = 'searched'
    
    // 如果需要在一段时间后恢复默认状态
    setTimeout(() => {
      searchState.value = 'default'
    }, 2000)
  }
}
</script>

<style lang="scss">
/* 自定义导航条 */
.navbar {
  background-image: url(@/static/images/navigator_bg.png);
  background-size: cover;
  position: relative;
  display: flex;
  flex-direction: column;
  padding-top: 20px;

  .logo {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 30rpx;

    .logo-image {
      width: 166rpx;
      height: 39rpx;
    }

    .logo-text {
      flex: 1;
      line-height: 28rpx;
      color: #fff;
      margin: 2rpx 0 0 20rpx;
      padding-left: 20rpx;
      border-left: 1rpx solid #fff;
      font-size: 26rpx;
    }
  }

  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10rpx 0 26rpx;
    height: 64rpx;
    margin: 16rpx 20rpx;
    color: #fff;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: rgba(255, 255, 255, 0.5);
    
    .search-input {
      flex: 1;
      background: transparent;
      border: none;
      color: #fff;
      font-size: 28rpx;
      padding: 10rpx 0;
      
      &::placeholder {
        color: rgba(255, 255, 255, 0.8);
      }
      
      // 去除输入框焦点时的默认边框
      &:focus {
        outline: none;
      }
    }
    
    .search-icon {
      width: 32rpx;
      height: 32rpx;
      padding: 10rpx 20rpx;
    }
  }
}
</style>